/**
 * Copyright 2022 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_QUANT_HELPER_GRAPH_INOUT_TRANSFORM_PASS_H_
#define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_QUANT_HELPER_GRAPH_INOUT_TRANSFORM_PASS_H_

#include <memory>
#include "ir/anf.h"
#include "ir/func_graph.h"
#include "ops/primitive_c.h"
#include "tools/converter/quantizer/quant_param_holder.h"
#include "tools/converter/quantizer/quantize_util.h"
#include "tools/converter/quantizer/quant_params.h"

namespace mindspore::lite::quant {
class GraphInoutTransformPass {
 public:
  explicit GraphInoutTransformPass(const FuncGraphPtr &func_graph, const std::shared_ptr<ConverterPara> param)
      : func_graph_(func_graph), param_(param) {
    CHECK_NULL_RETURN_VOID(this->param_);
    this->graph_input_dtype_ = static_cast<TypeId>(this->param_->input_data_type);
    this->graph_output_dtype_ = static_cast<TypeId>(this->param_->output_data_type);
  }

  ~GraphInoutTransformPass() = default;

  int Transform();

 private:
  int DoGraphInputDTypeTransform();

  int DoGraphOutputDTypeTransform();

  int InsertDTypeCastNode(const CNodePtr &cnode, size_t input_index, TypeId src_dtype, TypeId dst_dtype,
                          CastNodeType node_type);

  int DoOutputTransform(const CNodePtr &cnode, size_t index);

  bool IsGraphOutput(const CNodePtr &cnode);

  FuncGraphPtr func_graph_ = nullptr;
  std::shared_ptr<ConverterPara> param_ = nullptr;
  TypeId graph_input_dtype_ = TypeId::kTypeUnknown;
  TypeId graph_output_dtype_ = TypeId::kTypeUnknown;
};
}  // namespace mindspore::lite::quant
#endif  // MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_QUANT_HELPER_GRAPH_INOUT_TRANSFORM_PASS_H_
